﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using System.Data.OracleClient;
using Common.ADO.Tools;

namespace Common.Reflection.DAL.Managers
{
    /// <summary>
    /// 数据保存编辑Oracle帮助类
    /// </summary>
    public class OracleManager : Reflection.DAL.InterFace.DBInterFace
    {
        /// <summary>
        /// 返回LogeManager单列
        /// </summary>
        /// <returns></returns>
        public static OracleManager GetInstance()
        {
            return instance;
        }
        private static OracleManager instance = new OracleManager();
        private OracleManager() { }

        /// <summary>
        /// 查询返回表里的所有记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<object> GetList(object obj)
        {
            var tempobj = obj;
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            string sql = string.Format("select * from {0}", tableName);
            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //   string ClassName = obj.GetType().ToString();
            //   string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {
                //    object c = Assembly.Load(nameSpace).CreateInstance(ClassName);
                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();

            return tList;
        }

        /// <summary>
        /// 分页返回表里的所有记录
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="PageSize">每页信息量</param>
        /// <param name="NowPage">当前页</param>
        /// <returns></returns>
        public List<object> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount, object obj)
        {
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            PropertyInfo[] propertiesa = type.GetProperties();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            //SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM t_custome) A WHERE ROWNUM <= 4) WHERE RN >= 2;
            string sql = string.Format("select * from (select a.*, rownum rn from (select * from {0}  order by " + propertiesa[0].Name + " desc) a where rownum<={1}) where rn>={2}", tableName, PageSize * NowPage, ((NowPage - 1) * PageSize) + 1);

            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //   string ClassName = obj.GetType().ToString();
            //   string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {

                //   object c = Assembly.Load(nameSpace).CreateInstance(ClassName);
                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();
            recordCount = DBHelper.GetTableCount(tableName, "oracle", propertiesa[0].Name);
            if (recordCount % PageSize == 0)
            {
                PageCount = recordCount / PageSize;
            }
            else
            {
                PageCount = (recordCount / PageSize) + 1;
            }


            return tList;
        }

        /// <summary>
        /// 按条件分页返回表里的所有记录
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="PageSize">每页信息量</param>
        /// <param name="NowPage">当前页</param>
        /// <returns></returns>
        public List<object> GetListByPageAndKeys(int PageSize, int NowPage, object obj, out int PageCount, out int recordCount)
        {
            object tempobj = obj;
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);

            Type type2 = obj.GetType();
            PropertyInfo[] myPropertyInfo2 = type2.GetProperties();
            string sqlparam = "";
            foreach (PropertyInfo p in myPropertyInfo2)
            {
                if (!p.Name.Equals(myPropertyInfo2[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    if (p.PropertyType.ToString().Equals("System.DateTime"))
                    {
                        DateTime d = Convert.ToDateTime(p.GetValue(obj, null).ToString());
                        DateTime d2 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 00:00:00");
                        DateTime d3 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 23:59:59");
                        sqlparam += p.Name + "<to_date('" + d3.ToString() + "','yyyy-MM-dd HH24:mi:ss') and " + p.Name + ">to_date('" + d2.ToString() + "','yyyy-MM-dd HH24:mi:ss') and ";
                    }
                    else
                    {
                        if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                        {
                            sqlparam += p.Name + "='" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "' and ";
                        }
                    }
                }
            }
            sqlparam = sqlparam.Substring(0, sqlparam.Length - 4);

            string sql = string.Format("select * from (select a.*, rownum rn from (select * from {0} where {1}  order by " + myPropertyInfo2[0].Name + " desc) a where rownum<={2}) where rn>={3}", tableName, sqlparam, PageSize * NowPage, ((NowPage - 1) * PageSize) + 1);
            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //    string ClassName = obj.GetType().ToString();
            //    string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {
                //    object c = Assembly.Load(nameSpace).CreateInstance(ClassName);
                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();
            recordCount = DBHelper.GetTableCountByKeys(tableName, sqlparam, "oracle", myPropertyInfo2[0].Name);
            if (recordCount % PageSize == 0)
            {
                PageCount = recordCount / PageSize;
            }
            else
            {
                PageCount = (recordCount / PageSize) + 1;
            }


            return tList;
        }

        /// <summary>
        /// 查询按某条件查询的所有记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<object> GetAllByKey(string key, string value, object obj)
        {
            object tempobj = obj;
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            PropertyInfo[] propertie = type.GetProperties();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            string sql = string.Format("select  * from {0} where {1}='{2}' order by " + propertie[0].Name + " desc", tableName, key, value);
            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //   string ClassName = obj.GetType().ToString();
            //   string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {
                //   object c = Assembly.Load(nameSpace).CreateInstance(ClassName);
                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();

            return tList;
        }

        /// <summary>
        /// 查询满足条件的记录是否唯一存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool IsExist(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select count(" + myPropertyInfo[0].Name + ") from {0} where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (!p.Name.Equals(myPropertyInfo[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    if (p.PropertyType.ToString().Equals("System.DateTime"))
                    {
                        DateTime d = Convert.ToDateTime(p.GetValue(obj, null).ToString());
                        DateTime d2 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 00:00:00");
                        DateTime d3 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 23:59:59");
                        sql += p.Name + "<to_date('" + d3.ToString() + "','yyyy-MM-dd HH24:mi:ss') and " + p.Name + ">to_date('" + d2.ToString() + "','yyyy-MM-dd HH24:mi:ss') and ";
                    }
                    else
                    {
                        if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                        {
                            sql += p.Name + "='" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "' and ";
                        }
                    }
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            int id = Convert.ToInt32(OracleHelper.GetSingle(sql));
            //     Common.LogManager.GetInstance().Log(id.ToString());
            if (id == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 查询满足条件的记录是否存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool IsJustExist(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select count(" + myPropertyInfo[0].Name + ") from {0} where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (!p.Name.Equals(myPropertyInfo[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    if (p.PropertyType.ToString().Equals("System.DateTime"))
                    {
                        DateTime d = Convert.ToDateTime(p.GetValue(obj, null).ToString());
                        DateTime d2 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 00:00:00");
                        DateTime d3 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 23:59:59");
                        sql += p.Name + "<to_date('" + d3.ToString() + "','yyyy-MM-dd HH24:mi:ss') and " + p.Name + ">to_date('" + d2.ToString() + "','yyyy-MM-dd HH24:mi:ss') and ";
                    }
                    else
                    {
                        if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                        {
                            sql += p.Name + "='" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "' and ";
                        }
                    }
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            //   Common.LogManager.GetInstance().Log(sql);
            int id = Convert.ToInt32(OracleHelper.GetSingle(sql));
            if (id > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 查询所有满足某些条件的数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<object> GetSomeObject(object obj)
        {
            object tempobj = obj;
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select * from {0} where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (!p.Name.Equals(myPropertyInfo[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    if (p.PropertyType.ToString().Equals("System.DateTime"))
                    {
                        DateTime d = Convert.ToDateTime(p.GetValue(obj, null).ToString());
                        DateTime d2 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 00:00:00");
                        DateTime d3 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 23:59:59");
                        sql += p.Name + "<to_date('" + d3.ToString() + "','yyyy-MM-dd HH24:mi:ss') and " + p.Name + ">to_date('" + d2.ToString() + "','yyyy-MM-dd HH24:mi:ss') and ";
                    }
                    else
                    {
                        if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                        {
                            sql += p.Name + "='" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "' and ";
                        }
                    }
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            sql += " order by " + myPropertyInfo[0].Name + " desc";

            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //    string ClassName = obj.GetType().ToString();
            //    string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {
                //     object c = Assembly.Load(nameSpace).CreateInstance(ClassName);
                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();
            return tList;
        }

        public List<object> GetListByPageLikeKey(int PageSize, int NowPage, object obj, out int PageCount, out int recordCount)
        {
            object tempobj = obj;
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);

            Type type2 = obj.GetType();
            PropertyInfo[] myPropertyInfo2 = type2.GetProperties();
            string sqlparam = "";
            foreach (PropertyInfo p in myPropertyInfo2)
            {
                if (!p.Name.Equals(myPropertyInfo2[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                    {
                        sqlparam += p.Name + " like'%" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "%' and ";
                    }
                }
            }
            sqlparam = sqlparam.Substring(0, sqlparam.Length - 4);

            string sql = string.Format("select * from (select a.*, rownum rn from (select * from {0} where {1}  order by " + myPropertyInfo2[0].Name + " desc) a where rownum<={2}) where rn>={3}", tableName, sqlparam, PageSize * NowPage, ((NowPage - 1) * PageSize) + 1);
            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //   string ClassName = obj.GetType().ToString();
            //   string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {
                //    object c = Assembly.Load(nameSpace).CreateInstance(ClassName);

                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();
            recordCount = DBHelper.GetTableCountByKeys(tableName, sqlparam, "oracle", myPropertyInfo2[0].Name);
            if (recordCount % PageSize == 0)
            {
                PageCount = recordCount / PageSize;
            }
            else
            {
                PageCount = (recordCount / PageSize) + 1;
            }


            return tList;
        }
        /// <summary>
        /// 查询按某些条件查询的头几条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<object> GetTopList(int count, object obj)
        {
            object tempobj = obj;
            List<object> tList = new List<object>();
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select  top {0} * from {1} where ", count, obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (!p.Name.Equals(myPropertyInfo[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    if (p.PropertyType.ToString().Equals("System.DateTime"))
                    {
                        DateTime d = Convert.ToDateTime(p.GetValue(obj, null).ToString());
                        DateTime d2 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 00:00:00");
                        DateTime d3 = Convert.ToDateTime(d.Year + "-" + d.Month + "-" + d.Day + " 23:59:59");
                        sql += p.Name + "<to_date('" + d3.ToString() + "','yyyy-MM-dd HH24:mi:ss') and " + p.Name + ">to_date('" + d2.ToString() + "','yyyy-MM-dd HH24:mi:ss') and ";
                    }
                    else
                    {
                        if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                        {
                            sql += p.Name + "='" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "' and ";
                        }
                    }
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            sql += " order by " + myPropertyInfo[0].Name + " desc";

            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            //   string ClassName = obj.GetType().ToString();
            //   string nameSpace = obj.GetType().Namespace;
            while (reader.Read())
            {
                //    object c = Assembly.Load(nameSpace).CreateInstance(ClassName);
                var c = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = c.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;

                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], c, 0);
                    //    Common.LogManager.GetInstance().Log(reader[columnName].ToString());
                }

                tList.Add(c);
            }
            reader.Close();
            return tList;
        }

        /// <summary>
        /// 查询一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public object GetModel(object obj)
        {
            object tempobj = obj;
            Type type = obj.GetType();
            PropertyInfo[] propertie = type.GetProperties();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            string sql = string.Format("select * from {0} where " + propertie[0].Name + "='{1}'", tableName, propertie[0].GetValue(obj, null).ToString());
            OracleDataReader reader = OracleHelper.ExecuteReader(sql);
            while (reader.Read())
            {
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    string columnName = p.Name;
                    string PType = p.PropertyType.ToString();
                    DBHelper.Operator(p, PType, reader[columnName], tempobj, 0);
                }
            }
            reader.Close();
            return tempobj;
        }

        /// <summary>
        /// 保存oracle(ID无自动增长)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public int Insert(object obj)
        {
            int newId = -1;
            if (Reflection.DAL.Entity.ValidatePassed(obj))
            {
                Type type = obj.GetType();
                PropertyInfo[] myPropertyInfo = type.GetProperties();
                string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
                newId = GetMaxIdInOracle(tableName, myPropertyInfo[0].Name) + 1;
                //    Common.LogManager.GetInstance().Log(newId.ToString());
                string sql = string.Format("insert into {0} values(" + newId + ",", tableName);
                foreach (PropertyInfo p in myPropertyInfo)
                {
                    //       Common.LogManager.GetInstance().Log(p.PropertyType.ToString());
                    if (!p.Name.Equals(myPropertyInfo[0].Name))
                    {
                        if (p.PropertyType.ToString().Equals("System.DateTime"))
                        {
                            sql += "to_date('" + p.GetValue(obj, null) + "','yyyy-MM-dd HH24:mi:ss'),";
                        }
                        else if (p.PropertyType.ToString().Equals("System.String"))
                        {
                            if (p.GetValue(obj, null) != null)
                            {
                                if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                                {
                                    sql += "'" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "',";
                                }
                            }
                            else
                            {
                                sql += "'" + p.GetValue(obj, null) + "',";
                            }
                        }
                        else
                        {
                            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Money), true);
                            if (objs.Length > 0)
                            {
                                if (p.GetValue(obj, null) != null)
                                {
                                    sql += "'" + p.GetValue(obj, null).ToString().Replace(",", "") + "',";
                                }
                            }
                            else
                            {
                                sql += "'" + p.GetValue(obj, null) + "',";
                            }
                        }
                    }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql += ")";
                OracleHelper.ExecuteSql(sql);
            }
            return newId;
        }
        /// <summary>
        /// 测试表是否存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool TableIsExist(object obj)
        {
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            string sql = "select * from " + tableName + " where 1=2";
            OracleHelper.ExecuteSql(sql);
            return true;
        }


        /// <summary>
        /// 检测数据表是否能被插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public bool CouldInsert(object obj)
        {
            bool couldInsert = false;
            int newId = -1;
            if (Reflection.DAL.Entity.ValidatePassed(obj))
            {
                Type type = obj.GetType();
                PropertyInfo[] myPropertyInfo = type.GetProperties();
                string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
                newId = GetMaxIdInOracle(tableName, myPropertyInfo[0].Name) + 1;
                //    Common.LogManager.GetInstance().Log(newId.ToString());
                string sql = string.Format("insert into {0} values(-1,", tableName);
                foreach (PropertyInfo p in myPropertyInfo)
                {
                    //       Common.LogManager.GetInstance().Log(p.PropertyType.ToString());
                    if (!p.Name.Equals(myPropertyInfo[0].Name))
                    {
                        if (p.PropertyType.ToString().Equals("System.DateTime"))
                        {
                            sql += "to_date('" + p.GetValue(obj, null) + "','yyyy-MM-dd HH24:mi:ss'),";
                        }
                        else if (p.PropertyType.ToString().Equals("System.String"))
                        {
                            if (p.GetValue(obj, null) != null)
                            {
                                if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                                {
                                    sql += "'" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "',";
                                }
                            }
                            else
                            {
                                sql += "'" + p.GetValue(obj, null) + "',";
                            }
                        }
                        else
                        {
                            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Money), true);
                            if (objs.Length > 0)
                            {
                                if (p.GetValue(obj, null) != null)
                                {
                                    sql += "'" + p.GetValue(obj, null).ToString().Replace(",", "") + "',";
                                }
                            }
                            else
                            {
                                sql += "'" + p.GetValue(obj, null) + "',";
                            }
                        }
                    }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql += ")";
                try
                {
                    OracleHelper.ExecuteSql(sql);
                    couldInsert = true;

                }
                catch (Exception)
                {
                    couldInsert = false;
                }
                if (couldInsert)
                {
                    PropertyInfo[] infos = obj.GetType().GetProperties();
                    infos[0].SetValue(obj, -1, null);
                    ADO.DBManager.Delete(obj);
                }
            }
            return couldInsert;
        }

        /// <summary>
        /// 得到表的最大ID
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static int GetMaxIdInOracle(string tableName, string key)
        {
            string sql = string.Format("select max(" + key + ") from {0}", tableName);
            return Convert.ToInt32(OracleHelper.GetSingle(sql));
        }

        /// <summary>
        /// 编辑一个对象并保存
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="nameSpace"></param>
        public void Edit(object obj)
        {
            if (Reflection.DAL.Entity.ValidatePassed(obj))
            {
                Type type = obj.GetType();
                PropertyInfo[] myPropertyInfo = type.GetProperties();
                string sql = string.Format("update {0} set ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
                foreach (PropertyInfo p in myPropertyInfo)
                {
                    if (!p.Name.Equals(myPropertyInfo[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                    {
                        if (p.GetValue(obj, null) != null)
                        {
                            if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                            {
                                if (p.PropertyType.ToString().Equals("System.DateTime"))
                                {
                                    sql += p.Name + "=to_date('" + p.GetValue(obj, null) + "','yyyy-MM-dd HH24:mi:ss'),";
                                }
                                else if (p.PropertyType.ToString().Equals("System.String"))
                                {
                                    if (p.GetValue(obj, null) != null)
                                    {
                                        if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                                        {
                                            sql += p.Name + "='" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "',";
                                        }
                                    }
                                    else
                                    {
                                        sql += p.Name + "='" + p.GetValue(obj, null) + "',";
                                    }
                                }
                                else
                                {
                                    Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Money), true);
                                    if (objs.Length > 0)
                                    {
                                        if (p.GetValue(obj, null) != null)
                                        {
                                            sql += p.Name + "='" + p.GetValue(obj, null).ToString().Replace(",", "") + "',";
                                        }
                                    }
                                    else
                                    {
                                        sql += p.Name + "='" + p.GetValue(obj, null) + "',";
                                    }

                                }
                            }
                        }
                    }
                    else if (p.GetValue(obj, null) == null && p.PropertyType.ToString().Equals("System.String"))//添加部分
                    {
                        sql += p.Name + "='',";
                    }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql += " where " + myPropertyInfo[0].Name + "=" + myPropertyInfo[0].GetValue(obj, null);
                OracleHelper.ExecuteSql(sql);
            }
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="obj">对象</param>
        public void Delete(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("delete from {0} where " + myPropertyInfo[0].Name + "={1}", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1), myPropertyInfo[0].GetValue(obj, null));
            OracleHelper.ExecuteSql(sql);
        }
        /// <summary>
        /// 删除表里的所有内容
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteAll(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("delete from {0} ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            OracleHelper.ExecuteSql(sql);
        }

        /// <summary>
        /// 删除表里的几条数据（事务）
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteSome(object[] obj)
        {
            ArrayList al = new ArrayList();
            for (int i = 0; i < obj.Length; i++)
            {
                Type type = obj[i].GetType();
                PropertyInfo[] myPropertyInfo = type.GetProperties();
                al.Add(string.Format("delete from {0} where " + myPropertyInfo[0].Name + "={1}", obj[i].ToString().Substring(obj[i].ToString().LastIndexOf(".") + 1), myPropertyInfo[0].GetValue(obj[i], null)));
            }
            OracleHelper.ExecuteSqlTran(al);
        }

        /// <summary>
        /// 按照数据的某些字段的值删除记录
        /// </summary>
        /// <param name="obj">对象</param>
        public void DeleteColumByKey_Value(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("delete from {0} where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));

            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (!p.Name.Equals(myPropertyInfo[0].Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != "0001-1-1 0:00:00")
                {
                    sql += p.Name + "='" + p.GetValue(obj, null) + "' and ";
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            //     Common.LogManager.GetInstance().Log(sql);
            OracleHelper.ExecuteSql(sql);
        }
        /// <summary>
        /// 不改变原表字段改变列属性
        /// </summary>
        /// <param name="CMD"></param>
        public void EditTable(List<string> CMDS, object obj)
        {
            List<object> products = GetInstance().GetList(obj);
            DeleteAll(products[0]);
            for (int i = 0; i < CMDS.Count; i++)
            {
                ADO.Tools.OracleHelper.ExecuteSql(CMDS[i]);
            }

            for (int i = 0; i < products.Count; i++)
            {
                GetInstance().BackUpTableData(products[i]);
            }
        }

        /// <summary>
        /// 保存oracle(ID无自动增长)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        private int BackUpTableData(object obj)
        {

            int newId = -1;
            if (Reflection.DAL.Entity.ValidatePassed(obj))
            {
                Type type = obj.GetType();
                PropertyInfo[] myPropertyInfo = type.GetProperties();
                string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
                //    Common.LogManager.GetInstance().Log(newId.ToString());
                string sql = string.Format("insert into {0} values(", tableName);
                foreach (PropertyInfo p in myPropertyInfo)
                {
                    if (p.PropertyType.ToString().Equals("System.DateTime"))
                    {
                        sql += "to_date('" + p.GetValue(obj, null) + "','yyyy-MM-dd HH24:mi:ss'),";
                    }
                    else if (p.PropertyType.ToString().Equals("System.String"))
                    {
                        if (p.GetValue(obj, null) != null)
                        {
                            if (p.GetValue(obj, null).ToString().ToLower().IndexOf("<script") < 0)
                            {
                                sql += "'" + p.GetValue(obj, null).ToString().Replace("'", "\"") + "',";
                            }
                        }
                        else
                        {
                            sql += "'" + p.GetValue(obj, null) + "',";
                        }
                    }
                    else
                    {
                        sql += "'" + p.GetValue(obj, null) + "',";
                    }
                }

                sql = sql.Substring(0, sql.Length - 1);
                sql += ")";
                //     Common.LogManager.GetInstance().Log(sql);
                OracleHelper.ExecuteSql(sql);
            }
            return newId;
        }
    }
}
